package com.rclabs.preloaders.preloader1
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	
	public class ProgressBar extends Loader
	{
		public var timer:Timer;
		private var fadeOutRate:Number = .10;
		private var _ready:Boolean = false; 
		
		public static const FADE_OUT_COMPLETE:String = "fadeOutComplete";
		public function ProgressBar()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler, false, 0 ,true);
			
		}
		
		private function addToStageHandler(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			var url:String;
			if (this.stage.loaderInfo.parameters.hasOwnProperty("preloaderURL"))
			{
				url = this.stage.loaderInfo.parameters.preloaderURL;
			}
			else
			{
				url = "nahtspreloader.swf";
			}
			var urlRequest:URLRequest = new URLRequest(url)
			this.load(urlRequest);    
		}
		
		public function set ready(b:Boolean):void
		{
			
			
			var mc:MovieClip = content as MovieClip;
			
			if (b) {
				try{
					mc.ready = true;
					mc.addEventListener("animationComplete",closeScreen);
				}
				catch(e:Object)
				{
					_ready = b;
					closeScreen();
				}
			}
		}
		
		public function get ready():Boolean
		{
			return _ready;
		}
		
		public function set progress(n:Number):void
		{
			var mc:* = content as Object;
			try{
				mc.setProgress(n);
			}
			catch(e:Object)
			{
				
			}
			
		}
		
		public function closeScreen(event:Event=null):void
		{
			timer = new Timer(1);
			timer.addEventListener( TimerEvent.TIMER, closeScreenFade, false, 0, true );
			timer.start();
		}
		
		public function closeScreenFade( event:TimerEvent ):void
		{
			if( this.alpha > 0){
				this.alpha = this.alpha - fadeOutRate;
			} else {
				_ready = true;
				timer.stop();
				dispatchEvent(new Event(FADE_OUT_COMPLETE));
				this.parent.removeChild(this);
				
			}        
		}        
		
		
	}
}